import 'package:flutter/material.dart';
import 'package:flutter_web_study/provider/base_provider.dart';
import 'package:flutter_web_study/view_model/product_review_view_model.dart';
import '../api/app_config.dart';

///商品评论
class ProductReviewPage extends StatefulWidget {
  String? jsonBean;
  int tagPage = 0;

  ProductReviewPage({Key? key, this.tagPage = 0, this.jsonBean})
      : super(key: key);

  @override
  State<ProductReviewPage> createState() => _ProductReviewPageState();
}

class _ProductReviewPageState extends State<ProductReviewPage> {
  @override
  Widget build(BuildContext context) {
    return ProviderWidget<ProductReviewViewModel>(
        model: ProductReviewViewModel(
            context, widget.tagPage, widget.jsonBean ?? ""),
        onModelReady: (value) {
          value.initView();
        },
        builder: (context, viewModel, child) {
          return Scaffold(
            appBar: AppBar(
              title: Text("商品评价"),
            ),
            body: Column(
              mainAxisAlignment: MainAxisAlignment.start,
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
                Container(
                  padding: EdgeInsets.all(10),
                  child: Text(
                    "编辑评论",
                    style: TextStyle(fontSize: 20),
                  ),
                ),
                Container(
                  height: 400,
                  color: Colors.grey[100],
                  margin: EdgeInsets.all(10),
                  child: TextField(
                    minLines: 1,
                    maxLines: null,
                    controller: viewModel.controller,
                    decoration: InputDecoration(hintText: "请输入文字"),
                    onChanged: (value) {},
                  ),
                ),
                item2Widget(viewModel),
                MaterialButton(
                  color: Colors.blue,
                  child: Text("提交"),
                  onPressed: () {
                    viewModel.commentSure();
                  },
                )
              ],
            ),
          );
        });
  }

  Widget item2Widget(ProductReviewViewModel viewModel) {
    return Column(
      children: [
        Row(
          children: [
            SizedBox(width: 10),
            Text("评论图片"),
            Container(
              height: 110,
              child: ListView.builder(
                  shrinkWrap: true,
                  scrollDirection: Axis.horizontal,
                  itemCount: viewModel.imageList.length,
                  itemBuilder: (context, index) {
                    return Stack(
                      children: [
                        Container(
                          margin: EdgeInsets.all(5),
                          width: 200,
                          height: 200,
                          color: Colors.black,
                          child: Image.network(AppConfig.IMAGE_PREFIX +
                              viewModel.imageList[index]["image"]),
                        ),
                        Positioned(
                            child: IconButton(
                          icon: Icon(
                            Icons.dangerous,
                            color: Colors.blue,
                          ),
                          onPressed: () {
                            viewModel.deleteImageList(index);
                          },
                        ))
                      ],
                    );
                  }),
            ),
            IconButton(
                onPressed: () => viewModel.addImage(),
                color: Colors.red,
                icon: Icon(Icons.add)),
          ],
        ),
      ],
    );
  }
}
